<template>
  <view class="index">
    <view>
      <nut-button class="test-score" size="large">{{ radioVal }}</nut-button>
    </view>
    <view class="test-progress">
      <nut-progress
        :percentage="radioVal"
        stroke-color="linear-gradient(270deg, rgba(18,126,255,1) 0%,rgba(32,147,255,1) 32.815625%,rgba(13,242,204,1) 100%)"
        status="active"
        :is-show-percentage="false"
        :show-text="false"
        size="large"
      />
    </view>
    <view class="result-detail">
      <nut-cell-group title="测试结果" v-show="radioVal === score">
        <nut-cell>
          智商水平：{{ this.detail.label || '' }}
        </nut-cell>
        <nut-cell>
          注意：6个月内重复测试，会因为答案记忆导致测试结果产生偏差。
        </nut-cell>
      </nut-cell-group>
    </view>
  </view>
</template>

<script>
import Taro from '@tarojs/taro'
import './result.less'
import scoreJson from '@/static/test/score.json'
import standard from '@/static/test/standard.json'

export default {
  data() {
    return {
      params: {},
      radioVal: 0,
      score: 0,
      detail: {
        label: '',
      },
    }
  },
  methods: {
    setParams() {
      this.params = Taro.getCurrentInstance().router.params
      this.getDetail()
    },
    resetTest() {
      Taro.redirectTo({ url: '/pages/index/index' })
    },
    showScore() {
      let fn = () => {
        if (this.radioVal >= this.score) {
          clearInterval(timer)
        } else {
          this.radioVal++
        }
      }
      let timer = setInterval(fn, 100)
    },
    getScore(age, score) {
      var val = 0
      scoreJson.forEach(item => {
        let min = item.min
        let max = item.max
        if (age >= min && age < max) {
          item.score.forEach(i => {
            if (score >= i.min && score < i.max) {
              val = i.score
            }
          })
        }
      })
      return val
    },
    getDetail() {
      let score = this.getScore(parseFloat(this.params.age), parseInt(this.params.score))
      standard.forEach(item => {
        let min = item.min
        let max = item.max
        if (score >= min && score < max) {
          this.detail = item.data
        }
      })
      this.score = Math.ceil((parseInt(this.params.score) / 60) * this.detail.diff + this.detail.iq);
    }
  },
  mounted() {
    this.setParams()
    this.showScore()
  },
  onShareAppMessage() {
    return {
      title: '快来测测你的IQ吧',
      path: '/pages/index/index',
    }
  },
  onShareTimeline() {
    return {
      title: '快来测测你的IQ吧',
      path: '/pages/index/index',
    }
  }
}
</script>

<style lang="scss">
</style>
